/*
 * Copyright 2019,2020,2024 NXP
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef UWB_EVT_H
#define UWB_EVT_H

#include "phUwb_BuildConfig.h"

typedef enum
{
    USB_TLV_EVT,
} UWB_EvtType_t;

typedef enum
{
	Goldplate = 0x00,
	Referenceplate = 0x01,
	Pendingtestplate = 0x02
}IDENT_FLAG;

typedef struct
{
    uint16_t type;
    uint8_t *value;
    uint16_t size;

} tlv_t;

typedef enum
{
    gFlashSuccess_c = 0,
    gFlashInitError_c,
	gFlashReadError_c,
	gFlashWriteError_c,
	gFlashEraseError_c,
	gFlashVerifyError_c,
    gFlashParamInvalid_c,
} flashResult_t;

/* TLV types */
#define UCI_CMD        0x01
#define HBCI_CMD       0x02
#define HBCI_LAST_CMD  0x03
#define RESET          0x04
#define HBCI_QUERY_CMD 0x05
/** Deprecated. Use GET_VERISON_INFO instead */
#define GET_SOFTWARE_VERSION 0x06
#define GET_BOARD_ID         0x07
#define USB_LOOPBACK         0x08
#define MCU_RESET            0x09 // MCU nvReset will be called.
#define RCI_CMD              0x0A
#define SWITCH_PROTOCOL_CMD  0x0B
/** Deprecated. Use GET_VERISON_INFO instead */
#define GET_BOARD_VERSION 0x0C
#define GET_VERISON_INFO  0x0D

#define HDLL_CMD            0x0E
#define HDLL_EDL_LAST_WRITE 0x0F
#define HDLL_GET_NTF        0x10
#define HELIOS2_RESET       0x11

#define CHIPID_CMD 			0x20
#define MACREAD_CMD			0x21
#define IOTESTH_CMD			0x22
#define IOTESTL_CMD			0x23

#define CFO_CALIB			0x24
#define POW_CALIB			0x25
#define IDEN_CMD			0x26
#define QRCODE_CMD			0x27
#define TWR_CALIB			0x28
#define PDOA_CALIB			0x29
#define ROOT_CACERT			0x2A
#define LOCK_CERT			0x2B
#define LOCK_KEY			0x2C
#define BLE_BROADCAST_KEY	0x2D

#define CFOREAD_CMD 				0x31
#define POWREAD_CMD					0x32
#define IDENREAD_CMD 				0x33
#define QRCODEREAD_CMD				0x34
#define	TWRREAD_CMD		   			0x35
#define PDOAREAD_CMD				0x36
#define ROOT_CACERTREAD				0x37
#define LOCK_CERTREAD				0x38
#define LOCK_KEYREAD				0x39
#define BLE_BROADCAST_KEYREAD		0x3A
#define MCU_LOWPOWER				0x3B
#define IO_STATUS_test				0x3C
#define HPD_MODE					0x3D
#define REBOOT						0x3E
#define IFR_ERASE					0x3F

//Define OTA CMD
#define OTA_REQUEST_START_CMD     0x50 //上位机机请求开始OTA升级 命令格式为0x50 0x00 0x02 len(2byte固件长度)
#define OTA_RESPONSE_START_CMD    0x51 //模组响应请求开始,命令格式为0x51 0x00 0x01 0x00(成功)或者0x01(失败)
#define OTA_DATA_SEND_CMD         0x52 //上位机发送固件数据,命令格式为0x52 0x01 0x02  D0 ... D255 crc16
#define OTA_DATA_RESPOND_CMD      0x53 //模组响应,命令格式为0x53 0x00 0x01  0x00(成功)或者0x01(失败)
#define OTA_DATA_SEND_FINISH_CMD    0x54 //上位机发送OTA结束命令, 命令格式为0x54 0x00 0x02 CRC16(固件整体CRC16)
#define OTA_DATA_RESPONSE_FINESH_CMD   0x55 //模组响应OTA结束请求,进行整体固件的区域的回复和CRC校验,格式为0x55 0x00 0x01 0x00(成功)或者0x01(失败)
#define OTA_SET_BOOT_INFO_CMD     0x56 //上位机发送设置boot标志的命令,格式为0x56 0x00 0x01 0x00(不设置)或者0x01(设置)
#define OTA_RESPONSE_BOOT_INFO_CMD 0x57 //模组响应设置boot标志的命令,格式为0x57 0x00 0x01 0x00(成功)或者0x01(失败)
#define OTA_GET_FIRMWARE_VERSION 0x58 //获取版本号
#define OTA_RESPONSE_FIRMWARE_VERSION 0X59

//Define FLASH CMD
#define EXTERNAL_FLASH_TEST_CMD 0x90
#define EXTERNAL_FLASH_RESPONSE_TEST_CMD 0x91
#define INTERNAL_FLASH_TEST_CMD 0x92
#define INTERNAL_FLASH_RESPONSE_TEST_CMD 0x93


#define HDLL_RESET_TO_UCI 0x12

#define TLV_RESP_SIZE                      0x900
#define RESET_SOFTWARE_VERSION_SIZE        0x04
#define RESET_RESPONSE_SIZE                4
#define GET_SOFTWARE_VERSION_RESPONSE_SIZE 4
#define GET_CHIPID_SIZE					   18
#define GET_MAC_SIZE					   8
#define GET_VERISON_INFO_RESPONSE_SIZE                       \
    (00 + 1 /*< [0]  Tag   : GET_VERISON_INFO */             \
        + 1 /*< [1] SW VER : SW Major */                     \
        + 1 /*< [2] SW VER : SW Minor */                     \
        + 1 /*< [3] SW VER : SW Dev   */                     \
        + 1 /*< [4] SW VER : RFU */                          \
        + 1 /*< [5]   IC   : @ref UBWPnPUWBSIdentifier_t */  \
        + 1 /*< [6] Board  : @ref UBWPnPBoardIdentifier_t */ \
    )

#define UCI_MT_MASK     0xE0
#define UCI_MT_SHIFT    0x05
#define MT_ESE_CTRL_CMD 4
#define MT_ESE_CTRL_RSP 5
#define UCI_HEADER_SIZE 4

typedef enum UBWPnPUWBSIdentifier
{
    kUBWPnPUWBSIdentifier_SR100T = 00 + 1,
    kUBWPnPUWBSIdentifier_SR110T = 00 + 2,
    kUBWPnPUWBSIdentifier_SR150  = 00 + 3,
    kUBWPnPUWBSIdentifier_SR160  = 00 + 4,
    kUBWPnPUWBSIdentifier_SR100S = 00 + 5,
    kUBWPnPUWBSIdentifier_SR040  = 10 + 1,
    kUBWPnPUWBSIdentifier_SR200T = 20 + 1,
    kUBWPnPUWBSIdentifier_SR250  = 20 + 3,
} UBWPnPUWBSIdentifier_t;

typedef enum UBWPnPBoardIdentifier
{
    /* Rhodes V3 */
    kUBWPnPBoardIdentifier_SR1XX_RV3 /*             */ = 00 + 1,
    /* Rhodes V4 */
    kUBWPnPBoardIdentifier_SR1XX_RV4 /*             */ = 00 + 2,
    /* QN9090 + MK Shield */
    kUBWPnPBoardIdentifier_SR1XX_QN9090_ShieldV4 /* */ = 00 + 3,
    /* LPC55S + MK Shield */
    kUBWPnPBoardIdentifier_SR1XX_LPC55S_ShieldV4 /* */ = 00 + 4,
    /* SR040 Finder V3 */
    kUBWPnPBoardIdentifier_SR040_FV3 /*             */ = 10 + 1,
    kUBWPnPBoardIdentifier_SR2XX_Armado /*          */ = 20 + 1,
    kUBWPnPBoardIdentifier_SR2XX_Crete /*          */  = 20 + 2,
} UBWPnPBoardIdentifier_t;

#define MCTT_UCI_CMD       0
#define MCTT_ESE_CMD       1
#define UCI_HDR_LEN        4
#define UCI_EXT_LEN_IND    1
#define UCI_LEN_OFFSET     3
#define UCI_EXT_LEN_OFFSET 2
#define UCI_EXT_LEN_MASK   0x80

void UWB_HandleEvt(UWB_EvtType_t ev, void *args);

size_t UWBPnP_GetVersionInfo(uint8_t *tlvBuf, size_t tlvBufLen, UBWPnPBoardIdentifier_t board);

#endif // UWB_EVT_H
